﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using GroupBox = System.Windows.Forms.GroupBox;
using NCHQMES;
using System.Xml.Linq;

namespace NCHQMES
{
    public partial class FrmMesMachineInfo : Form
    {
        private MachineInfo machineInfo = null;

        public FrmMesMachineInfo()
        {
            InitializeComponent();
            EnableValue(MainPanel, false);
            machineInfo = new MachineInfo();
            try
            {
                machineInfo = Method.JsonToObject<MachineInfo>(Method.ReadPath<MachineInfo>());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            if (machineInfo == null)
            {
                machineInfo = new MachineInfo();
            }
            ControlValue(MainPanel, "Set");
        }

        private void btnRecompose_Click(object sender, EventArgs e)
        {
            EnableValue(MainPanel);
            btnRecompose.BackColor = Color.Red;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ControlValue(MainPanel, "Get");
            try
            {
                if (Method.WritePathTxt<MachineInfo>(Method.ObjectToJson<MachineInfo>(machineInfo)))
                {
                    MessageBox.Show("保存成功");
                    EnableValue(MainPanel, false);
                    btnRecompose.BackColor = Color.WhiteSmoke;
                    //Method.HeartBeatMache = Method.JsonToObject<MachineInfo>(Method.ReadPath<MachineInfo>());
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败:" + ex.Message.ToString(), "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 启用/屏蔽控件
        /// </summary>
        /// <param name="objControl">控件</param>
        /// <param name="flag">Enable值</param>
        private void EnableValue(Control objControl, bool flag = true)
        {
            try
            {
                foreach (Control control in objControl.Controls)
                {
                    if (control is GroupBox)
                    {
                        GroupBox group = (GroupBox)control;
                        EnableValue(group, flag);
                    }
                    if (control is TextBox)
                    {
                        TextBox textbox = (TextBox)control;
                        textbox.Enabled = flag;
                    }
                    if (control is CheckBox)
                    {
                        CheckBox checkBox = (CheckBox)control;
                        checkBox.Enabled = flag;
                    }
                    if (control is Button)
                    {
                        Button button = (Button)control;
                        if (button.Name.Contains("Interface"))
                        {
                            button.Enabled = flag;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("控件启用方法:{private void EnableValue(Control objControl, bool flag = true)}出错" + "错误信息:" + ex.Message.ToString(), "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 获取/设置控件值
        /// </summary>
        /// <param name="objControl">控件</param>
        /// <param name="objStr">Get:获取、Set:设置</param>
        private void ControlValue(Control objControl, string objStr)
        {
            try
            {
                FieldInfo[] fields = machineInfo.GetType().GetFields();
                foreach (FieldInfo field in fields)
                {
                    Control[] control = objControl.Controls.Find(field.Name, true);
                    if (control[0] is CheckBox)
                    {
                        CheckBox checkBox = (CheckBox)control[0];
                        switch (objStr)
                        {
                            case "Get":
                                field.SetValue(machineInfo, checkBox.Checked);

                                break;

                            case "Set":
                                object obj = field.GetValue(machineInfo);
                                checkBox.Checked = (bool)obj;
                                break;

                            default:
                                break;
                        }
                    }
                    if (control[0] is TextBox)
                    {
                        TextBox textbox = (TextBox)control[0];
                        switch (objStr)
                        {
                            case "Get":
                                if (field.FieldType.Name == "Int32")
                                {
                                    field.SetValue(machineInfo, Convert.ToInt32(textbox.Text));
                                }
                                else
                                {
                                    field.SetValue(machineInfo, textbox.Text);
                                }

                                break;

                            case "Set":
                                object obj = field.GetValue(machineInfo);
                                textbox.Text = obj.ToString();
                                break;

                            default:
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取/设置控件值:{private void ControlValue(Control objControl, string objStr)}出错" + "错误信息:" + ex.Message.ToString(), "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnInterfaceTest_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(Method.MES_SCADA_FormateDemo());
            }
            catch (Exception ex)
            {
                MessageBox.Show("测试接口错误信息" + ex.Message.ToString());
            }
        }

        private void btnHeartbeat_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(Method.MES_SCADA_Heartbeat());
            }
            catch (Exception ex)
            {
                MessageBox.Show("心跳接口错误信息" + ex.Message.ToString());
            }
        }

        private void btnWarning_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(Method.MES_SCADA_Warning("Produce", "1001", User.Text));
                MessageBox.Show(Method.MES_SCADA_Warning("Eliminate", "1001", User.Text));
            }
            catch (Exception ex)
            {
                MessageBox.Show("报警接口错误信息" + ex.Message.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(Method.MES_SCADA_Startup(User.Text));
            }
            catch (Exception ex)
            {
                MessageBox.Show("软件打开时接口错误信息" + ex.Message.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(Method.MES_SCADA_ScrewWorkData(new SCADA_ScrewWorkData.Write()));
            }
            catch (Exception ex)
            {
                MessageBox.Show("螺丝机工作接口错误信息" + ex.Message.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(Method.MES_SCADA_StatusChange("Runing", User.Text));
            }
            catch (Exception ex)
            {
                MessageBox.Show("状态改变接口错误信息" + ex.Message.ToString());
            }
        }

        private void FrmMesMachineInfo_Load(object sender, EventArgs e)
        {

        }
    }
}